package com.cx.mall.user.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.user.dto.UserReceiveAddressDTO;
import com.cx.mall.user.entity.UserReceiveAddress;
import com.cx.mall.user.mapper.IUserReceiveAddressMapper;
import com.cx.mall.user.service.IUserReceiveAddressService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserReceiveAddressServiceImpl extends ServiceImpl<IUserReceiveAddressMapper, UserReceiveAddress> implements IUserReceiveAddressService {

    @Override
    public List<UserReceiveAddress> listAddressByUserId(Long id) {
        LambdaQueryChainWrapper<UserReceiveAddress> query = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        List<UserReceiveAddress> list = query.eq(UserReceiveAddress::getUserId, id).list();
        return list;
    }

    @Override
    public List<UserReceiveAddressDTO> listByUserId(Long id) {
        LambdaQueryChainWrapper<UserReceiveAddress> query = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        List<UserReceiveAddressDTO> collect = query.eq(UserReceiveAddress::getUserId, id).list().
                stream().map(e -> JSON.parseObject(JSON.toJSONString(e), UserReceiveAddressDTO.class))
                .collect(Collectors.toList());
        return collect;
    }

}
